﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

public partial class profile : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            retreive();
        }
    }
    protected void retreive()
    {
        int ret_code;
        SqlCommand cmd = new SqlCommand();
        DataTable dtRet = new DataTable();
        cmd.CommandText = "get_profile";
        SqlParameter para = cmd.Parameters.AddWithValue("@uid", Session["UID"].ToString());
        ret_code = TinyDAL.Exec(ref cmd, ref dtRet);
        if (ret_code == 1)
        {
            lblError.Text = "未发现用户！";
            btnUpdate.Enabled = false;
            return;
        }
        DataRow row = dtRet.Rows[0];
        txtUid.Text = row["uid"].ToString();
        txtName.Text = row["name"].ToString();
        if (Convert.ToBoolean(row["male"]))
        {
            radMale.Checked = true;
            radFemale.Checked = false;
        }
        else
        {
            radMale.Checked = false;
            radFemale.Checked = true;
        }
        txtEmail.Text = row["email"].ToString();
        txtMobilePhone.Text = row["mobile_phone"].ToString();
        txtDob.Text = Convert.ToDateTime(row["dob"]).ToShortDateString();
        txtRegisterDate.Text = Convert.ToDateTime(row["register_date"]).ToShortDateString();
        txtPoints.Text = row["total_points"].ToString();
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        int ret_code;
        txtPwd.Text = txtPwd.Text.Trim();
        txtPwd2.Text = txtPwd2.Text.Trim();
        if (txtPwd.Text.Length < 6)
        {
            lblError.Text = "密码长度至少要达到6位！";
            return;
        }
        if (txtPwd.Text != txtPwd2.Text)
        {
            lblError.Text = "两次输入密码不同，请重新输入。";
            return;
        }
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "update_profile";
        SqlParameter para = cmd.Parameters.AddWithValue("@uid", Session["UID"].ToString());
        para = cmd.Parameters.AddWithValue("@pwd", txtPwd.Text);
        para = cmd.Parameters.AddWithValue("@email", txtEmail.Text);
        para = cmd.Parameters.AddWithValue("@mobile_phone", txtMobilePhone.Text);
        ret_code = TinyDAL.Exec(ref cmd);
        if (ret_code == 0)
        {
            lblError.Text = "更新成功！";
        }
        else
        {
            lblError.Text = "更新失败！";
        }

    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        retreive();
    }
}
